import React, { Component } from 'react'
import {ListBox} from '../../../components'
import UpdateFactoryPage from './Update'
import AssignFactoryUser from './AssignFactoryUser'
import AssignFactoryRes from './AssignFactoryRes'
import AsignSapFactory from './AsignSapFactory'
import { Modal } from 'antd';

export default class ListFactoryPage extends Component {
    constructor(props) {
        super(props);
        //行操作列表
        this.rowOperation = [];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'addFactory',
                showName: '新增',
                iconClass: 'plus'
            },{
                handleName: 'edit',
                showName: '修改',
                iconClass: 'edit'
            },{
                handleName: 'delete',
                showName: '删除',
                iconClass: 'delete'
            },{
                handleName: 'assignUser',
                showName: '分配用户',
                iconClass: 'key'
            },
            // {
            //     handleName: 'assignResource',
            //     showName: '分配资源',
            //     iconClass: 'key'
            // },
            {
                handleName: 'assignSap',
                showName: '分配SAP工厂',
                iconClass: 'key'
            }
        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'ListFactoryColumnConfig',
            data: [
                {
                    showName: 'factoryId',
                    columnName: 'factoryId',
                    columnHidden: true,
                    foreverHidden: true
                },
                {
                    showName: '工厂编码',
                    columnName: 'factoryCode',
                    columnWidth: 130
                }, {
                    showName: '工厂名称',
                    columnName: 'factoryName'
                }, {
                    showName: '工厂类型',
                    columnName: 'factoryType',
                    showOption:[
                        {
                            optionKey:'0',
                            optionShow:'生产工厂'
                        },{
                            optionKey:'1',
                            optionShow:'外库工厂'
                        }
                    ]                    
                },{
                    showName: '是否开启原材料库存',
                    columnName: 'isOpenStorage',
                    columnWidth: 150,
                    showOption:[
                        {
                            optionKey:'0',
                            optionShow:'否'
                        },{
                            optionKey:'1',
                            optionShow:'是'
                        }
                    ]                      
                },{
                    showName: '是否开启供应商库存',
                    columnName: 'isOpenSupply',
                    columnWidth: 150,
                    showOption:[
                        {
                            optionKey:'0',
                            optionShow:'否'
                        },{
                            optionKey:'1',
                            optionShow:'是'
                        }
                    ]                      
                },{
                    showName: '是否开启质检状态',
                    columnName: 'isOpenHmqm',
                    columnWidth: 150,
                    showOption:[
                        {
                            optionKey:'0',
                            optionShow:'否'
                        },{
                            optionKey:'1',
                            optionShow:'是'
                        }
                    ]                      
                },{
                    showName: '是否开启下线结算',
                    columnName: 'isOpenWkSeq',
                    columnWidth: 150,
                    showOption:[
                        {
                            optionKey:'0',
                            optionShow:'否'
                        },{
                            optionKey:'1',
                            optionShow:'是'
                        }
                    ]                      
                },{
                    showName: '归属产业',
                    columnName: 'groupCode',
                    columnWidth: 130,
                },{
                    showName: 'SAP事业部编码',
                    columnName: 'deptCode',
                    columnWidth: 130,
                },{
                    showName: '提货点',
                    columnName: 'userDefined3' 
                }, {
                    showName: '创建人',
                    columnName: 'createBy'
                }, {
                    showName: '创建时间',
                    columnName: 'gmtCreate'
                }, {
                    showName: '更新人',
                    columnName: 'lastModifiedBy'
                }, {
                    showName: '更新时间',
                    columnName: 'gmtModified'
                }/*,{
                 showName: '有效标志',
                 columnName: 'activeFlag'
                 }*/
            ]
        };

        //搜索属性配置
        this.searchComponentData = [
            {
                name:'gmtCreate',
                type: 'date',
                maxName:'gmtCreateMax',
                minName:'gmtCreateMin'
            },{
                name:'gmtModified',
                type: 'date',
                maxName:'gmtModifiedMax',
                minName:'gmtModifiedMin'
            },{
                name : 'factoryType',
                type : 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'生产工厂'
                    },{
                        optionKey:'1',
                        optionShow:'外库工厂'
                    }
                ]
            },{
                name : 'isOpenStorage',
                type : 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'否'
                    },{
                        optionKey:'1',
                        optionShow:'是'
                    }
                ]
            },{
                name : 'isOpenSupply',
                type : 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'否'
                    },{
                        optionKey:'1',
                        optionShow:'是'
                    }
                ]
            },{
                name : 'isOpenHmqm',
                type : 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'否'
                    },{
                        optionKey:'1',
                        optionShow:'是'
                    }
                ]
            },{
                name : 'isOpenWkSeq',
                type : 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'否'
                    },{
                        optionKey:'1',
                        optionShow:'是'
                    }
                ]
            }
        ];

    }

    rowHandles = (strHandles, rowObj) => {
        console.log("strHandles===ListResource=======", strHandles);
        if (strHandles === 'edit') {
            this.editHandle(rowObj);
        } else if (strHandles === 'delete') {
            this.deleteHandle(rowObj);
        } else if (strHandles === 'addFactory') {
            this.addFactoryHandle();
        } else if (strHandles == 'assignUser') {
            this.assignFactoryUserHandle(rowObj);
        }else if(strHandles === 'assignResource'){
            this.assignFactoryResHandle(rowObj);
        }else if(strHandles === 'assignSap'){
            this.assignSapHandle(rowObj);
        }
    }
    // 增加工厂
    addFactoryHandle = () => {
        console.log("====执行了新增工厂操作====");
        this.refs.updateFactory.getFormData[1].disable = false;
        this.refs.updateFactory.addFactoryHandle();
    }
    editHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            // this.refs.updateFactory.getFormData[5].disable = true;
            this.refs.updateFactory.getFormData[1].disable = true;
            this.refs.updateFactory.editHandle(rowObj[0].data);
        }
    }

    deleteHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updateFactory.deleteHandle(rowObj[0].data)
        }
    }
    // 给工厂分配角色
    assignFactoryUserHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            //传递参数--工厂id
            let factoryId = rowObj[0].data.factoryId;
            this.refs.assignFactoryUser.initModal(factoryId);
        }
        
        
    }

    // 给工厂分配资源
    assignFactoryResHandle = (rowObj) => {
        console.log("====执行了分配资源操作====");
        // 只能选择一条限制
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            //待分配角色
            let factoryId = rowObj[0].data.factoryId;
            //待分配角色拥有的资源id数据
            //const ownResIds = ['2'];
            this.refs.assignFactoryRes.initModal(factoryId);
        }
    }

    //分配sap工厂
    assignSapHandle = (rowObj) => {
        console.log("====执行了分配sap工厂操作====");
        // 只能选择一条限制
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.asignSapFactory.editHandle(rowObj)
        }
    }
    //刷新列表
    refreshHandle = () => {
        this.refs.listFactory.refreshHandle();
    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return (
            <div>
                <ListBox
                    ref='listFactory'
                    isHideLeft={isHideLeft}
                    tableColumnData={this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url='/base/factory/list'
                    title="工厂管理"
                    rowHandles={this.rowHandles}
                    rowOperation={this.rowOperation}
                    headerOperation={this.headerOperation}
                    isRowSingle = "true"
                />

                <UpdateFactoryPage
                    ref="updateFactory"
                    refreshHandle={this.refreshHandle}
                />
                <AssignFactoryUser
                    ref="assignFactoryUser"
                    refreshHandle={this.refreshHandle}
                />
                <AssignFactoryRes
                    ref="assignFactoryRes"
                    refreshHandle={this.refreshHandle}
                />
                <AsignSapFactory
                    ref="asignSapFactory"
                    refreshHandle={this.refreshHandle}
                />
            </div>
        )
    }
}
